package com.fwtai.service.web;

import com.fwtai.bean.PageFormData;
import com.fwtai.bean.UploadFile;
import com.fwtai.bean.UploadObject;
import com.fwtai.config.ConfigFile;
import com.fwtai.service.AsyncService;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import com.fwtai.tool.ToolString;
import com.fwtai.web.CarouselDao;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 轮播图业务层
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2022-06-01 16:12:26
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class CarouselService{

  @Value("${upload_dir_window}")
  private String dir_window;

  @Value("${upload_dir_linux}")
  private String dir_linux;

  private final CarouselDao carouselDao;
  private final AsyncService asyncService;

  public CarouselService(final CarouselDao carouselDao,final AsyncService asyncService){
    this.carouselDao = carouselDao;
    this.asyncService = asyncService;
  }

  public String add(final HttpServletRequest request){
    final PageFormData formData = new PageFormData(request);
    final String p_title = "title";
    final String p_url = "url";
    final String p_category = "category";
    final String p_img_path = "img_path";
    final String validate = ToolClient.validateField(formData,p_title,p_img_path);
    if(validate != null)return validate;
    if(formData.getString(p_title).length() > 64){
      return ToolClient.createJsonFail("标题内容字数太长");
    }
    final String v_category = formData.getString(p_category);
    if(v_category != null){
      final Integer category = formData.getInteger(p_category);
      if(category == null){
        return ToolClient.createJsonFail("轮播图分类有误");
      }
    }
    final String url = formData.getString(p_url);
    if(url != null && url.length() > 128){
      return ToolClient.createJsonFail("跳转的url内容字数太长");
    }
    if(formData.getString(p_img_path).length() > 256){
      return ToolClient.createJsonFail("全路径内容字数太长");
    }
    final String baseDir = ToolString.isLinuxOS() ? dir_linux : dir_window;
    final UploadObject uploadObject = ToolClient.uploadFile(request,baseDir,"/law/carousel",false,1,384,true,".jpg",".png",".mp4");
    final String errorMsg = uploadObject.getErrorMsg();
    if(errorMsg != null) return ToolClient.createJsonFail(errorMsg);
    final ArrayList<UploadFile> listFiles = uploadObject.getListFiles();
    if(listFiles == null || listFiles.size() <= 0) return ToolClient.createJsonFail("请选择上传文件");
    final UploadFile uploadFile = listFiles.get(0);
    formData.put("img_url",uploadFile.getUrlFile());
    formData.put("img_path",uploadFile.getFullPath());
    formData.put("kid",new IdBuilder().getId121L());
    return ToolClient.executeRows(carouselDao.add(formData));
  }

  public String edit(final HttpServletRequest request){
    final PageFormData formData = new PageFormData(request);
    final String p_kid = "kid";
    final String p_title = "title";
    final String p_category = "category";
    final String p_url = "url";
    final String p_img_path = "img_path";
    final String validate = ToolClient.validateField(formData,p_title,p_kid);
    if(validate != null)return validate;
    final String v_title = formData.getString(p_title);
    if(v_title != null && v_title.length() > 64){
      return ToolClient.createJsonFail("标题内容字数太长");
    }
    final String v_category = formData.getString(p_category);
    if(v_category != null){
      final Integer category = formData.getInteger(p_category);
      if(category == null){
        return ToolClient.createJsonFail("轮播图分类有误");
      }
    }
    final String v_url = formData.getString(p_url);
    if(v_url != null && v_url.length() > 128){
      return ToolClient.createJsonFail("跳转的url内容字数太长");
    }
    final String v_img_path = formData.getString(p_img_path);
    if(v_img_path != null && v_img_path.length() > 256){
      return ToolClient.createJsonFail("全路径内容字数太长");
    }
    final String baseDir = ToolString.isLinuxOS() ? dir_linux : dir_window;
    final UploadObject uploadObject = ToolClient.uploadFile(request,baseDir,"/law/carousel",false,1,384,false,".jpg",".png",".mp4");
    final String errorMsg = uploadObject.getErrorMsg();
    if(errorMsg != null) return ToolClient.createJsonFail(errorMsg);
    final ArrayList<UploadFile> listFiles = uploadObject.getListFiles();
    if(listFiles != null && listFiles.size() > 0){
      final UploadFile uploadFile = listFiles.get(0);
      formData.put("img_url",uploadFile.getUrlFile());
      formData.put("img_path",uploadFile.getFullPath());
    }
    final Long exist_key = carouselDao.queryExistById(formData.getLong(p_kid));
    if(exist_key == null){
      return ToolClient.createJsonFail("数据不存在,刷新重试");
    }
    return ToolClient.executeRows(carouselDao.edit(formData));
  }

  public String queryById(final Long kid){
    if(kid == null){
      return ToolClient.jsonValidateField();
    }
    return ToolClient.queryJson(carouselDao.queryById(kid));
  }

  //隐藏数据
  public String delById(final Long kid){
    if(kid == null){
      return ToolClient.jsonValidateField();
    }
    return ToolClient.executeRows(carouselDao.delById(kid),"操作成功","隐藏失败,数据不存在,刷新重试");
  }

  //删除数据
  public String delData(final Long kid){
    if(kid == null){
      return ToolClient.jsonValidateField();
    }
    final String url = carouselDao.getFilePath(kid);
    final int rows = carouselDao.delData(kid);
    if(rows > 0){
      if(url != null){
        asyncService.delLocalFile(url);
      }
    }
    return ToolClient.executeRows(rows,"操作成功","数据不存在,刷新重试");
  }

  public String editEnable(final Long kid){
    if(kid == null){
      return ToolClient.jsonValidateField();
    }
    return ToolClient.executeRows(carouselDao.editEnable(kid),"操作成功","显示失败,数据不存在,刷新重试");
  }

  public String delByKeys(final String ids){
    if(ids == null || ids.length() <= 0){
      return ToolClient.jsonValidateField();
    }
    final long[] lists = ToolString.keysToArarry(ids);
    if(lists == null){
      return ToolClient.createJsonFail("请选择要隐藏的数据");
    }
    return ToolClient.executeRows(carouselDao.delByKeys(lists),"操作成功","数据不存在,刷新重试");
  }

  public String listData(PageFormData formData){
    try{
      formData = ToolClient.dataTableMysql(formData);
      if(formData == null)return ToolClient.jsonValidateField();
      final HashMap<String,Object> map = carouselDao.listData(formData);
      return ToolClient.dataTableOK((List<?>)map.get(ConfigFile.rows),map.get(ConfigFile.total));
    } catch (final Exception ignored){
      return ToolClient.dataTableException();
    }
  }
}